#include "ProjectArtColor.h"

//https://www.bilibili.com/video/BV1DM4y1n7eB/?spm_id_from=333.337.search-card.all.click&vd_source=be4b42409fb0d5ad1e1bd5a5715b7da0
ProjectArtColor::ProjectArtColor(const std::string &name, CC::Scene *scene)
	: ProjectSandbox(name, scene)
{
	CC_INFO("-----ProjectArtColor----\n");
}

ProjectArtColor::~ProjectArtColor()
{
}

void ProjectArtColor::OnAttach()
{
    ProjectSandbox::OnAttach();

    CC::Shader* shader = CC::ResourceManager::LoadShader("Renderer2D_QuadArtColor", ("Renderer2D_QuadArtColor"), ("Renderer2D_QuadArtColor"));
    CC::ShaderComponent* shader_sc = new CC::ShaderComponent(shader);

    CC::Custom2DMaterialComponent* custom2DMaterialComponent = new CC::Custom2DMaterialComponent();
    custom2DMaterialComponent->datas["TextureSize0"] = CC::Custom2DMaterialComponent::DataInfo(CC::Custom2DMaterialComponent::DataType_Float2, GetWidth(), GetHeight());
    custom2DMaterialComponent->datas["Time"] = CC::Custom2DMaterialComponent::DataInfo(CC::Custom2DMaterialComponent::DataType_Float, 0.0f);
  
    GetNode()->AddComponent<CC::ShaderComponent>(shader_sc);
    GetNode()->AddComponent<CC::Custom2DMaterialComponent>(custom2DMaterialComponent);

    GetAnimObj()->SetAnimTime(4000.0);
}

void ProjectArtColor::OnDetach()
{
    ProjectSandbox::OnDetach();
}

void ProjectArtColor::OnUpdate(CC::Timestep ts, std::string renderscene)
{        
    ProjectSandbox::OnUpdate(ts, renderscene);

    if (0 == GetShowWin()) return;

    auto tran = GetNode()->GetComponent<CC::TransformComponent>(CC::ComponentType_Transform);
    tran->Translation = glm::vec3(0.0, 0.0, 0.0);
    tran->Scale = glm::vec3(GetWidth(), GetHeight(), 0.0);

    auto custom2DMaterialComponent = GetNode()->GetComponent<CC::Custom2DMaterialComponent>(CC::ComponentType_Custom2DMaterial);
    custom2DMaterialComponent->datas["TextureSize0"] = CC::Custom2DMaterialComponent::DataInfo(CC::Custom2DMaterialComponent::DataType_Float2, GetWidth(), GetHeight());

    time += ts.GetMilliseconds() * 0.001;
    custom2DMaterialComponent->datas["Time"] = CC::Custom2DMaterialComponent::DataInfo(CC::Custom2DMaterialComponent::DataType_Float, time);

}